package com.zznode.redsun.commons.dto;

import java.util.ArrayList;
import java.util.Collection;

import com.zznode.csp.domain.commons.util.DomainBeanUtils;
import com.zznode.redsun.commons.utils.GlobalUtils;
import com.zznode.redsun.smartcrm.model.Customer;
import com.zznode.redsun.smartcrm.model.CustomerLevel;
import com.zznode.redsun.smartcrm.model.CustomerType;
import com.zznode.redsun.smartcrm.model.MonthlyIncome;
import com.zznode.redsun.smartcrm.model.PersonalHobby;
import com.zznode.redsun.smartcrm.model.PersonalInfo;
import com.zznode.redsun.smartcrm.model.Profession;

public class CustomerDto extends BaseDto {
	private String typeId;
	private String typeName;
	private String levelId;
	private String levelName;
	private String incomeId;
	private String incomeName;
	private String professionId;
	private String professionName;
	private String createUserId;
	private String createUserName;
	private Long carmodelId;
	private String carmodelName;
	private Long serviceUserId;
	private String serviceUserName;
	private Boolean receiveDoc;
	private PersonalDto personal;
	private Collection<String> hobbies;

	public static CustomerDto create(Customer entity) {
		CustomerDto customer = new CustomerDto();
		PersonalDto personal = PersonalDto.create(entity.getPersonalInfo());
		customer.setParentField(entity);
		customer.setPersonal(personal);
		if (null != entity.getType()) {
			customer.setTypeId(entity.getType().getId());
			customer.setTypeName(entity.getType().getName());
		}
		if (null != entity.getLevel()) {
			customer.setLevelId(entity.getLevel().getId());
			customer.setLevelName(entity.getLevel().getName());
		}
		if (null != entity.getMonthlyIncome()) {
			customer.setIncomeId(entity.getMonthlyIncome().getId());
			customer.setIncomeName(entity.getMonthlyIncome().getName());
		}
		if (null != entity.getProfession()) {
			customer.setProfessionId(entity.getProfession().getId());
			customer.setProfessionName(entity.getProfession().getName());
		}
		customer.setReceiveDoc(entity.getReceiveDoc());

		if (null != entity.getHobbies() && !entity.getHobbies().isEmpty()) {
			for (PersonalHobby entry : entity.getHobbies()) {
				customer.addHobby(entry);
			}
		}

		return customer;
	}

	public static CustomerDto create() {
		CustomerDto customer = new CustomerDto();
		PersonalDto personal = new PersonalDto();
		customer.setPersonal(personal);
		customer.setCode(GlobalUtils.generateCode(null));
		customer.setLevelId("10");
		customer.setTypeId("10");
		customer.getPersonal().setCardCategoryId("10");

		return customer;
	}

	public Customer convert() {
		Customer entity = new Customer();
		PersonalInfo info = new PersonalInfo();
		entity.setPersonalInfo(info);

		update(entity);
		entity.getPersonalInfo().setCode(entity.getCode());
		entity.getPersonalInfo().setName(entity.getName());
		return entity;
	}

	public void update(Customer entity) {
		super.update(entity);
		personal.update(entity.getPersonalInfo());
		entity.setType(DomainBeanUtils.create(CustomerType.class, typeId));
		entity.setLevel(DomainBeanUtils.create(CustomerLevel.class, levelId));
		entity.setMonthlyIncome(DomainBeanUtils.create(MonthlyIncome.class, incomeId));
		entity.setProfession(DomainBeanUtils.create(Profession.class, professionId));
		entity.setReceiveDoc(receiveDoc);
		if (null != entity.getHobbies()) {
			entity.getHobbies().clear();
		}
		for (String entry : hobbies) {
			entity.addHobby(DomainBeanUtils.create(PersonalHobby.class, entry));
		}
	}

	public Collection<String> getHobbies() {
		return hobbies;
	}

	public void setHobbies(Collection<String> hobbies) {
		this.hobbies = hobbies;
	}

	public void addHobby(PersonalHobby hobby) {
		if (null == this.hobbies) {
			this.hobbies = new ArrayList<String>();
		}
		if (!this.hobbies.contains(hobby)) {
			this.hobbies.add(hobby.getId());
		}
	}

	public PersonalDto getPersonal() {
		return personal;
	}

	public void setPersonal(PersonalDto personal) {
		this.personal = personal;
	}

	public String getTypeId() {
		return typeId;
	}

	public void setTypeId(String typeId) {
		this.typeId = typeId;
	}

	public String getTypeName() {
		return typeName;
	}

	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}

	public String getLevelId() {
		return levelId;
	}

	public void setLevelId(String levelId) {
		this.levelId = levelId;
	}

	public String getLevelName() {
		return levelName;
	}

	public void setLevelName(String levelName) {
		this.levelName = levelName;
	}

	public String getIncomeId() {
		return incomeId;
	}

	public void setIncomeId(String incomeId) {
		this.incomeId = incomeId;
	}

	public String getIncomeName() {
		return incomeName;
	}

	public void setIncomeName(String incomeName) {
		this.incomeName = incomeName;
	}

	public String getProfessionId() {
		return professionId;
	}

	public void setProfessionId(String professionId) {
		this.professionId = professionId;
	}

	public String getProfessionName() {
		return professionName;
	}

	public void setProfessionName(String professionName) {
		this.professionName = professionName;
	}

	public Boolean getReceiveDoc() {
		return receiveDoc;
	}

	public void setReceiveDoc(Boolean receiveDoc) {
		this.receiveDoc = receiveDoc;
	}

	public String getCreateUserId() {
		return createUserId;
	}

	public void setCreateUserId(String createUserId) {
		this.createUserId = createUserId;
	}

	public String getCreateUserName() {
		return createUserName;
	}

	public void setCreateUserName(String createUserName) {
		this.createUserName = createUserName;
	}

	public Long getCarmodelId() {
		return carmodelId;
	}

	public void setCarmodelId(Long carmodelId) {
		this.carmodelId = carmodelId;
	}

	public String getCarmodelName() {
		return carmodelName;
	}

	public void setCarmodelName(String carmodelName) {
		this.carmodelName = carmodelName;
	}

	public Long getServiceUserId() {
		return serviceUserId;
	}

	public void setServiceUserId(Long serviceUserId) {
		this.serviceUserId = serviceUserId;
	}

	public String getServiceUserName() {
		return serviceUserName;
	}

	public void setServiceUserName(String serviceUserName) {
		this.serviceUserName = serviceUserName;
	}
}
